<?php

/**
 * 缓存 ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang@fistforward.com
 */

class ajax_cacheManage{

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1:
                    $this->getList();
                    break;
                case 2:
                    $this->deleteCache();
                    break;
            }
        }
    }

    private function getList(){

        if(!isset($_REQUEST['group']) || empty($_REQUEST['group']))
            die(json_encode(array('code'=>400,'message'=>'参数错误')));

        $group = trim($_REQUEST['group']);

        $dir = G_APPLICATION_BASEPATH . $group . '/html_cache/';

        $result = @$this->getDir($dir);

        $list = array();

        if(is_array($result) && !is_null($result[0])){

            foreach($result as $key => $val){

                $list[$key]['id'] = $key;
                $list[$key]['group'] = $group;

                switch($val){
                    case 'goods':
                        $list[$key]['name'] = '商品详情页';
                        break;
                    case 'index':
                        $list[$key]['name'] = '首页';
                        break;
                    case 'contentCenter':
                        $list[$key]['name'] = '内容查询中心';
                        break;
                    case 'article':
                        $list[$key]['name'] = '文章';
                        break;
                    case 'new_product':
                        $list[$key]['name'] = '最新产品';
                        break;
                    default:
                        $list[$key]['name'] = '未定义';
                        break;
                }

                $list[$key]['value'] = $val;
                $list[$key]['path'] = $dir.$val;
            }
        }

        die(json_encode(array('code'=>200,'data'=>$list)));

    }


    private function deleteCache(){

        if(!isset($_REQUEST['group'],$_REQUEST['project']) || empty($_REQUEST['group']) || empty($_REQUEST['project']))
            die(json_encode(array('code'=>400,'message'=>'参数错误')));

        $group = trim($_REQUEST['group']);

        $project = trim($_REQUEST['project']);

        $dir = G_APPLICATION_BASEPATH . $group . '/html_cache/'.$project;

        if(is_dir($dir)){
            if($this->delDir($dir)){
                die(json_encode(array('code'=>200,'message'=>'删除成功！','data'=>$dir)));
            }else{
                die(json_encode(array('code'=>400,'message'=>'删除失败！','data'=>$dir)));
            }
        }else{
            die(json_encode(array('code'=>400,'message'=>'目录不存在！')));
        }

    }

    /**
     * 获取文件目录列表,该方法返回数组
     * getDir()去文件夹列表，getFile()去对应文件夹下面的文件列表,二者的区别在于判断有没有“.”后缀的文件，其他都一样     *
     */

    public function getDir($dir) {

        $dirArray[] = NULL;
        if (false != ($handle = opendir ( $dir ))) {
            $i=0;
            while ( false !== ($file = readdir ( $handle )) ) {
                //去掉"“.”、“..”以及带“.xxx”后缀的文件
                if ($file != "." && $file != ".."&&!strpos($file,".")) {
                    $dirArray[$i]=$file;
                    $i++;
                }
            }
            //关闭句柄
            closedir ( $handle );
        }
        return $dirArray;
    }

    /**
     * 获取文件列表,该方法返回数组
     */

    public function getFile($dir) {

        $fileArray[] = NULL;

        if (false != ($handle = opendir ( $dir ))) {
            $i=0;
            while ( false !== ($file = readdir ( $handle )) ) {
                //去掉"“.”、“..”以及带“.xxx”后缀的文件
                if ($file != "." && $file != ".."&&strpos($file,".")) {
                    $fileArray[$i]="./shop/html_cache/".$file;
                    if($i==100){
                        break;
                    }
                    $i++;
                }
            }
            //关闭句柄
            closedir ( $handle );
        }
        return $fileArray;
    }


    /**
     * 删除文件夹及其文件夹下所有文件
     */
    private function delDir($dir) {
        //先删除目录下的文件：
        $dh = opendir($dir);
        while ($file=readdir($dh)) {
            if($file != "." && $file != "..") {
                $fullpath = $dir."/".$file;
                if(!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->delDir($fullpath);
                }
            }
        }
        closedir($dh);
        //删除当前文件夹：
        if(rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }
}